//
//  PersonalCenterVC.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/8/3.
//  Copyright (c) 2015年 szb. All rights reserved.
//

#import "PersonalCenterVC.h"

#import "PCLabelItem.h"
#import "PersonCenterGroup.h"
#import "PCArrowItm.h"
#import "PersonCenterCellView.h"
#import "AccountTool.h"
#import "personInfoViewController.h"
#import "addressMangerVC.h"
#import "productCenterVC.h"
#import "orderCenterVC.h"
#import "shareCenterVC.h"
#import "BPLoginViewController.h"
#import "BPhttpTool.h"
#import "BPImageTool.h"
#import "BaseWebViewController.h"
#import "AppDelegate.h"

@interface PersonalCenterVC ()

@end

@implementation PersonalCenterVC

- (void)viewWillAppear:(BOOL)animated
{
    [self setUppersonData:[AccountTool account]];
    [self setUpDownload];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[self setUpDownload];
    
    [self setUpTopInfo];
    
    [self setUpTableView];
}

- (void)setUpDownload
{
    [BPhttpTool get:[NSString stringWithFormat:@"%@user/numinfo/uid/%@",BPhttp,[AccountTool account].uid] params:nil success:^(id responseObj) {
        [self setUpTopNumber:responseObj];
    } failure:^(NSError *error) {
        BPLog(@"获取数量失败");
    }];
}

- (void)setUpTopInfo
{
    _topInfo = [[PersonCenterTopInfo alloc] initWithFrame:CGRectMake(0, BPScreenNavY, BPScreenW, 110)];
    [_topInfo.myProductBtn addTarget:self action:@selector(gotoProductCenter) forControlEvents:UIControlEventTouchUpInside];
    [_topInfo.myOrderBtn addTarget:self action:@selector(gotoOrderCenter) forControlEvents:UIControlEventTouchUpInside];
    [_topInfo.myShareBtn addTarget:self action:@selector(gotoShareCenter) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_topInfo];
}

- (void)setUpTopNumber:(NSDictionary *)numberDic{
    _topInfo.myProductBtn.numberLabel.text = numberDic[@"wnum"];
    _topInfo.myOrderBtn.numberLabel.text = numberDic[@"onum"];
    _topInfo.myShareBtn.numberLabel.text = numberDic[@"wnum"];
}

- (void)setUppersonData:(BPAccount *)account
{
    _topInfo.userName.text = account.nickname;
    _topInfo.userNumber.text = account.mobile;
    if (account.imageData != nil) {
        _topInfo.userPic.image = [UIImage imageWithData:account.imageData];
    }else if (account.face != nil){
        [BPImageTool setUpUIImageView:_topInfo.userPic addNsstringUrl:account.face];
    }
}

- (void)gotoProductCenter
{
    productCenterVC *productCenter = [[productCenterVC alloc] init];
    productCenter.title = @"我的作品";
    [self.navigationController pushViewController:productCenter animated:YES];
}

- (void)gotoOrderCenter
{
    orderCenterVC *orderCenter = [[orderCenterVC alloc] init];
    orderCenter.title = @"我的订单";
    [self.navigationController pushViewController:orderCenter animated:YES];
}

- (void)gotoShareCenter
{
    shareCenterVC *shareCenter = [[shareCenterVC alloc] init];
    shareCenter.title = @"我的分享";
    [self.navigationController pushViewController:shareCenter animated:YES];
}

- (void)setUpTableView
{
    CGFloat tableY = CGRectGetMaxY(_topInfo.frame) - 90.0;
    self.tableView.frame = CGRectMake(0, tableY, BPScreenW, BPScreenH - tableY);
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self setUpGroups];
}

- (void)setUpGroups
{
    PersonCenterGroup *group = [PersonCenterGroup group];
    
    [self.groups addObject:group];
    
    PCArrowItm *AllIndent = [PCArrowItm itewWithTitle:@"全部订单"];
    AllIndent.destVcClass = [orderCenterVC class];
    
    PCArrowItm *PersonInfo = [PCArrowItm itewWithTitle:@"个人信息"];
    PersonInfo.destVcClass = [personInfoViewController class];
    
    PCArrowItm *Saddress = [PCArrowItm itewWithTitle:@"收货管理地址"];
    Saddress.destVcClass = [addressMangerVC class];
    
    PCArrowItm *Voucher = [PCArrowItm itewWithTitle:@"我的美印币"];
    Voucher.operation = ^{
        BaseWebViewController *webVC = [[BaseWebViewController alloc] init];
        webVC.webName = @"meiyinbi.html";
        webVC.title = @"美印币";
        [self.navigationController pushViewController:webVC animated:YES];
    };
    group.items = @[AllIndent,PersonInfo,Saddress,Voucher];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
